using System;
using TinyServerLib;
using TinyLogger;

namespace DtGoogleServer
{
	/// <summary>
	/// Host the Google Desktop Server Proxy host
	/// </summary>
	class GoogleServerHost
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			try
			{
				GoogleServer server = new GoogleServer();
				configure(server);
				server.Run();
				Console.WriteLine("Server running. Hit Enter to exit");
				Console.ReadLine();
				server.Stop();
				Console.WriteLine("Server stopped successfully");
			}
			catch (Exception ex)
			{
				Console.WriteLine("Listener died with exception {0}", ex.Message);
				Console.WriteLine(ex.StackTrace);
			}
			Console.WriteLine("-- hit enter to exit --");
			Console.ReadLine();		
		}
		private static void configure(TinyServerLib.TinyServer server)
		{
            server.ServerLogger.AddLoggerSink(new ConsoleLogger());
            server.ServerLogger.SetLevel(LogLevel.Informational);

			server.Templates   = System.Configuration.ConfigurationManager.AppSettings["TemplatePath"];

            server.DefaultPage = System.Configuration.ConfigurationManager.AppSettings["Default"];
            server.WebRootPath = System.Configuration.ConfigurationManager.AppSettings["WebRoot"];
            server.Port = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Port"]);
            string logLevel = System.Configuration.ConfigurationManager.AppSettings["LogLevel"].ToUpper();
			switch (logLevel) 
			{
                case "ALL": server.ServerLogger.SetLevel(LogLevel.Informational);
                    break;
                case "WARNING": server.ServerLogger.SetLevel(LogLevel.Warning);
                    break;
                case "ERROR": server.ServerLogger.SetLevel(LogLevel.Error);
                    break;
                case "NONE": server.ServerLogger.SetLevel(LogLevel.Fatal);
                    break;
			}
		}

	}
}
